<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use QCloud_WeApp_SDK\Auth\LoginService as LoginService;
use QCloud_WeApp_SDK\Constants as Constants;
use QCloud_WeApp_SDK\Auth\AuthAPI as Auth;
use QCloud_WeApp_SDK\Mysql\Mysql as DB;

class Login extends CI_Controller {
    public function index() {
        $result = LoginService::login();
        if ($result['loginState'] === Constants::S_AUTH) {
            $this->json([
                'code' => 0,
                'data' => $result['userinfo']
            ]);
        } else {
            $this->json([
                'code' => -1,
                'error' => $result['error']
            ]);
        }
    }



// 登录验证openid
public function getopid()
{
 // $wx_user_code = $_GET['code'];
  $code = $this->input->get('code');
  $URL = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxa0ec8fd85b25f5c2&secret=51d4c6e3af88497a8b58caf92b61d0a8&js_code=' .$code . '&grant_type=authorization_code';
  $info = file_get_contents($URL);
  $json = json_decode($info);
  $arr = get_object_vars($json);
  if (array_key_exists('openid', $arr)){
    $res['status'] = 200;
    $res['openid'] = $arr['openid'];

} else {
    $res['start'] = 500;
}
echo json_encode($res);
}



// 验证用户有无登录记录
public function ceck_openid()
{
  $openid = $this->input->get('openid');
  $record=db::select('userinfo',['*'],'oppenid="'.$openid.'" ');
  if (count($record != 0)) {
    $res['status'] = 200;
    $res['data'] = $record;
  } else {
    $res['status'] = 401;
    $res['data'] = '错误的用户编码格式';
  }
  
  echo json_encode($res);
  
}
 










 public function testlog(){
     $code = $this -> input -> get('code');
     echo $code;
    }

    public function testlogin()
    {
        $result = LoginService::login();
        echo json_encode($result);
    }



}
